package com.stone.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.stone.bean.vo.LoginVO;
import com.stone.constant.Const;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

@Slf4j
@Component
public class SpringContextConfig implements ApplicationContextAware {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ApplicationConfig applicationConfig;
    private static ApplicationContext applicationContext;
    private static RedisTemplate staticRedisTemplate;

    private void init() {
        log.info("============ 项目启动环境：{} =============", applicationConfig.getEnvironment());
        try {
            redisTemplate.opsForValue().get("aaa");
            log.warn("===============================================================");
            log.warn("======================== redis success ========================");
            log.warn("===============================================================");
        } catch (Exception e) {
            log.warn("=============================================================");
            log.warn("======================== redis error ========================");
            log.warn("=============================================================");
        }
        if (SpringContextConfig.isTest()) {
            String swaggerUrl = String.format("http://localhost:%s%s/swagger-ui.html", applicationConfig.getPort(), applicationConfig.getContextPath());
            log.info("============ swagger地址：{} =============", swaggerUrl);
        }
        staticRedisTemplate = redisTemplate;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextConfig.applicationContext = applicationContext;
        init();
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

    public static boolean isProd() {
        ApplicationConfig config = (ApplicationConfig) getBean("applicationConfig");
        return "prod".equals(config.getEnvironment());
    }

    public static boolean isTest() {
        return !isProd();
    }

    public static boolean isWindows() {
        return System.getProperties().getProperty("os.name").toUpperCase().contains("WINDOWS");
    }

    public static LoginVO loginUser(String token) {
        if (StrUtil.isBlank(token)) {
            return null;
        }
        Object o = staticRedisTemplate.opsForValue().get(Const.AUTH_PREFIX + token);
        if (Objects.nonNull(o)) {
            LoginVO loginVO = JSON.parseObject(o.toString(), LoginVO.class);
            return loginVO;
        }
        return null;
    }

    public static Long loginUserId(String token) {
        LoginVO loginVO = loginUser(token);
        if (Objects.nonNull(loginVO)) {
            return loginVO.getId();
        }
        return null;
    }

    public static String loginUserName(String token) {
        LoginVO loginVO = loginUser(token);
        if (Objects.nonNull(loginVO)) {
            return loginVO.getUserName();
        }
        return null;
    }
}